// import Token from '../../utils/token';
// import { PASSWORDERROR, USERNOEXIST } from '../../utils/error';
const assert = require('assert');
// const macObj = require('getmac');
const BasicController = require('../BasicController.js');
const fs = require('fs');
const path = require('path');
const rename = think.promisify(fs.rename, fs);
module.exports = class extends BasicController {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      data: this.post(),
      id: this.getId()
    };
  }
  async addAction() {
    const params = this.params;
    // console.log(params.data);
    const data = await this.model(params.model).addAction(params);
    return this.success(data);
  }
  async editAction() {
    const params = this.params;
    const data = await this.model(params.model).editAction(params);
    return this.success(data);
  }
  // async listAction(){
  //   const params = {
  //     model: this.MODEL,
  //   };
  //   const data = await this.model(this.MODEL).listAction(params);
  //   return this.success(data);
  // }
  async delAction() {
    const params = this.params;
    const data = await this.model(params.model).delAction(params);
    // console.log(data);
    return this.success(data);
  }
  async selectdataAction() {
    const params = this.params;
    let newmodel;
    switch (params.data.classifyid) {
      case '1':
        newmodel = 'group_classify'; // 组别筛选
        break;
      default:
        newmodel = 'student_info';
        break;
    }
    const dataes = await this.model(newmodel).getField(params.data.classify);
    const arrlist = Array.from(new Set([...dataes]));
    const arrdata = [];
    for (const i in arrlist) {
      const obj = {};
      const obj2 = {};
      obj2[params.data.classify] = arrlist[i];
      if (params.data.classifyid === '1') {
        const iddata = await this.model(newmodel).where({stu_group: arrlist[i]}).find();
        obj[arrlist[i]] = await this.model('student_info').where({group_id: iddata.id}).select();
      } else {
        obj[arrlist[i]] = await this.model(newmodel).where(obj2).select();
      }
      arrdata.push(obj);
    }
    return this.success(arrdata);
  }
  async addInfoAction() {
    const params = {
      data: this.params.data,
      model: 'richtext'
    };
    const id = think.uuid('v4');
    params.data.id = id;
    const data = await this.model(params.model).add(params.data);
    this.success(data);
  }
  async delInfoAction() {
    const params = {
      data: this.params.data,
      model: 'richtext'
    };
    const data = await this.model(params.model).where({relation_id: params.data.id}).delete();
    const imgdata = await this.model('introduction_files').where({relation_id: params.data.id}).delete();
    this.success({data, imgdata});
  }
  async getInfoAction() {
    const params = this.params;
    const groupData = await this.model(params.model).where({id: params.id}).find();
    if (think.isEmpty(groupData)) {
      return this.fail('查询id不存在');
    }
    const data = await this.model('richtext').where({relation_id: params.id}).find();
    const imgdata = await this.model('introduction_files').where({relation_id: params.id}).select();
    if (!think.isEmpty(data.richtext)) {
      data.richtext = data.richtext.toString('utf-8');
      groupData.groupInfo = data;
    } else {
      groupData.groupInfo = {};
    }
    groupData.imgdata = imgdata;
    return this.success(groupData);
  }
  async listallInfoAction() {
    const params = this.params;
    const data = await this.model(params.model).listallInfoAction(params);
    this.success(data);
  }
  async editInfoAction() {
    const params = {
      data: this.params.data,
      model: 'richtext'
    };
    const data = await this.model(params.model).where({relation_id: params.data.id}).find();
    let databack = '';
    if (!think.isEmpty(data)) {
      databack = await this.model(params.model).where({relation_id: params.data.id}).update(params.data);
    } else {
      const id = think.uuid('v4');
      params.data.relation_id = params.data.id;
      params.data.id = id;
      databack = await this.model(params.model).add(params.data);
    }
    return this.success(databack);
  }
  async imgUploadAction() {
    const data = this.params.data;
    const file = this.file().file;
    const filedata = {};
    const firstname = think.md5(file.name);
    const lastname = path.extname(file.name);
    const name = `${firstname}${lastname}`;
    const filepath = path.join(think.ROOT_PATH, `runtime/upload/introductionFiles/${data.relation_id}/${name}`);
    think.mkdir(path.dirname(filepath));
    await rename(file.path, filepath);
    // 上传单张图片
    const fileid = think.uuid('v4');
    filedata.id = fileid;
    filedata.file_path = filepath;
    filedata.relation_id = data.relation_id;
    await this.model(this.MODEL).add(filedata);
    this.success({id: fileid}, 'ok');
  }
  async delimgAction() {
    const params = this.params;
    const backdata = await this.model(params.model).where(params.data).delete();
    this.success(backdata, 'ok');
  }
};

/**
 * @api {POST} /group/group_classify/addInfo /addInfo
 * @apiName addInfo
 * @apiGroup groupInfo
 * @apiParam {String} relation_id 分组ID
 * @apiParam {String} [richtext] 分组信息－富文本

 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                   relation_id:1,
                   richtext:[Blob],
                  }
 */
/**
 * @api {DELETE} /group/group_classify/delInfo /delInfo
 * @apiName delInfo
 * @apiGroup groupInfo
 * @apiParam {String} id 分组ID

 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {del} Request-Example:
                 {
                   id: *********,
                  }
 */
/**
 * @api {GET} /group/group_classify/getInfo /getInfo
 * @apiName getInfo
 * @apiGroup groupInfo
 * @apiParam {String} id 分组ID
 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   relation_id:1,
                  }
 */
/**
 * @api {POST} /group/group_classify/listallInfo /listallInfo
 * @apiName listallInfo
 * @apiGroup groupInfo
 * @apiParam {String} [stu_group] 组名
 * @apiParam {String} [teacher_name] 负责教师id
 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": [
        {
            "id": "1d89470c-33f2-407e-8330-2ad43e8847b7", //图片ID
            "teacher_name": "2",
            "headman_name": "2",
            "stu_group": "web",
            "richtext": "<p>asdf</p>",
            "relation_id": "16ea1833-9d1c-11e8-9fcb-525400180e73", //分组ID
            "status": 0,
            "file_path": "/home/d/Code/mobile_internet_website_server/runtime/upload/introductionFiles/16ea1833-9d1c-11e8-9fcb-525400180e73/1017564-z.jpg"
        }
    ]
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                  }
 */
/**
 * @api {POST} /group/group_classify/editInfo /editInfo
 * @apiName editInfo
 * @apiGroup groupInfo
 * @apiParam {String} id 分组ID
 * @apiParam {String} [richtext] 富文本
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                   id: ******,
                   richtextL:[]
                  }
 */
/**
 * @api {POST} /group/group_classify/imgUpload /imgUpload
 * @apiName imgUpload
 * @apiGroup groupInfo
 * @apiParam {String} relation_id 分组ID
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": {
      id:******************
    }
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": {}
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                   relation_id: ******,
                  }
 */
/**
 * @api {POST} /group/group_classify/delimg /delimg
 * @apiName delimg
 * @apiGroup groupInfo
 * @apiParam {String} id 图片ID
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": {
      id:******************
    }
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": {}
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                   id: ******,
                  }
 */
/**
 * @api {POST} /group/group_classify/add /add
 * @apiName add
 * @apiGroup group
 * @apiParam {String} teacher_name 分组负责教师
 * @apiParam {String} [headman_name] 组长账号
 * @apiParam {String} stu_group 组名

 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                   teacher_name:1,
                   stu_group:"Web",
                  }
 */
/**
 * @api {POST} /group/group_classify/selectdata /selectdata
 * @apiName selectdata
 * @apiGroup group
 * @apiParam {String} classifyid 根据所选分类id查询
 * @apiParam {String} classify 具体查询字段

 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": [
        {
            "web": [
                {
                    "Stu_UserID": "3",
                    "Stu_Num": "5120162135",
                    "groupid": "16ea1833-9d1c-11e8-9fcb-525400180e73",
                    "Stu_Name": "张家鑫",
                    "Stu_Grade": "16级",
                    "Stu_Major": "软件工程",
                    "Stu_Class": "11",
                    "Stu_Tel": "18800000000",
                    "Stu_IDCard": "510613199808208888",
                    "Stu_BankCard": "",
                    "Stu_Email": "dywzzjx@163.com",
                    "teacher_name": "1",
                    "Stu_Level": "1",
                    "Stu_create_time": "2018-08-05 14:21:12",
                    "Stu_update_time": "2018-08-11 13:09:16",
                    "role_id": "1"
                }
            ]
        },
        {
            "ios": [
                {
                    "Stu_UserID": "1",
                    "Stu_Num": "5120168888",
                    "groupid": "16ea1eaf-9d1c-11e8-9fcb-525400180e73",
                    "Stu_Name": "zjx",
                    "Stu_Grade": "11",
                    "Stu_Major": "1",
                    "Stu_Class": "11",
                    "Stu_Tel": "",
                    "Stu_IDCard": "1",
                    "Stu_BankCard": "",
                    "Stu_Email": "1",
                    "teacher_name": "XXB",
                    "Stu_Level": "1",
                    "Stu_create_time": "2018-08-05 14:21:40",
                    "Stu_update_time": "2018-08-11 13:09:27",
                    "role_id": "1"
                }
            ]
        }
    ]
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                   classifyid:1,
                   classyfi:"stu_group",
                  }
 */
/**
  * @api {POST} /group/group_classify/edit /edit
 * @apiName del
 * @apiGroup group
 * @apiParam {String} id 修改信息的组ID
 * @apiParam {String} [teacher_name] 负责教师
 * @apiParam {String} [headman_name] 组长账号
 * @apiParam {String} [stu_group] 组名
 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 1
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *          "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                   id:1,
                   teacher_name:2,
                   stu_group:"Web",
                 }
  */

/**
  * @api {DELETE} /group/group_classify/del /del
 * @apiName edit
 * @apiGroup group
 * @apiParam {String} id 修改信息的组ID
 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 1
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *          "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {del} Request-Example:
                 {
                   id:1,
                 }
  */

/**
  * @api {get} /group/group_classify/get /get
 * @apiName get
 * @apiGroup group
 * @apiParam {String} id 根据id查询
 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       "errno": 0,
    "errmsg": "ok",
    "data": [
        {
            "id": 1,
            "teacher_name": 1,
            "headman_name": 5120152008,
            "stu_group": "Web"
        }
    ]
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *          "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiExample {String} Request-Example:
*                 ?id:1
  */

/**
  * @api {POST} /group/group_classify/listall /listall
 * @apiName listall
 * @apiGroup group

 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
     "errno": 0,
    "errmsg": "",
    "data": [
        {
            "id": 1,
            "teacher_name": 1,
            "headman_name": 5120152008,
            "stu_group": "Web"
        },
        {
            "id": 2,
            "teacher_name": 2,
            "headman_name": 5120152424,
            "stu_group": "Android"
        },
        {
            "id": 3,
            "teacher_name": 2,
            "headman_name": 5120152424,
            "stu_group": "Android"
        },
        {
            "id": 4,
            "teacher_name": 2,
            "headman_name": 5120152424,
            "stu_group": "Android"
        },
        {
            "id": 5,
            "teacher_name": 2,
            "headman_name": 5120152424,
            "stu_group": "Android"
        },
        {
            "id": 6,
            "teacher_name": 3,
            "headman_name": 5120152424,
            "stu_group": "Android"
        },
        {
            "id": 7,
            "teacher_name": 1,
            "headman_name": 5120152008,
            "stu_group": "Web"
        },
        {
            "id": 8,
            "teacher_name": 1,
            "headman_name": 5120152008,
            "stu_group": "Web"
        }
    ]
*
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *          "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                 }
  */
